<template>
	<view class="home-container">
		<bgImg imgUrl="../../static/home/topTitle.png"></bgImg>
		<image class="home-container-list" src="../../static/home/list.png" mode=""></image>
		<!-- <image class="home-container-packet" src="../../static/home/packet.png" mode=""></image> -->
		<bottomFill></bottomFill>
		<tabBar :selectIndex="0"></tabBar>
	</view>
</template>

<script>
	export default {
		onLoad(){
			// uni.setStorage({
			// 	key:"gameStatus",
			// 	data:"success"
			// })
			// 路由守卫
			uni.getStorage({
			    key: 'isLogin',
			    fail: function (res) {
			        // console.log(res.data);
					uni.redirectTo({
						url:"/pages/login/login"
					})
			    }
			});
			
			// webSocket
			let socketOpen = false;
			let socketMsgQueue = [];
			// webSocket配置
			const socketTask = uni.connectSocket({
			    url: 'wss://yf.obtdata.com/c/nyfws',
				complete: (r)=> {
					console.log(r)
				}
			    // data() {
			    //     return {
			    //         x: '',
			    //         y: ''
			    //     };
			    // },
			    // header: {
			    //     'content-type': 'application/json'
			    // },
			    // protocols: ['protocol1'],
			    // method: 'GET'
			});
			// 监听WebSocket连接打开事件
			uni.onSocketOpen(function (res) {
			  console.log('WebSocket连接已打开！', res);
			  
			  socketOpen = true;
			  for (var i = 0; i < socketMsgQueue.length; i++) {
			    sendSocketMessage(socketMsgQueue[i]);
			  }
			  socketMsgQueue = [];
			})
			
			function sendSocketMessage(msg) {
			  if (socketOpen) {
			    uni.sendSocketMessage({
			      data: msg
			    });
			  } else {
			    socketMsgQueue.push(msg);
			  }
			}

			// 监听WebSocket错误
			uni.onSocketError(function (res) {
			  console.log('WebSocket连接打开失败，请检查！', res);
			});
			
			// 监听WebSocket接受到服务器的消息事件
			uni.onSocketMessage(function (res) {
			  console.log('收到服务器内容：' + res.data);
			  let dataObj = JSON.parse(res.data);
			  
			  // 开始游戏
			  if(dataObj.data == 'startGame'){
				// 获取游戏状态
				uni.setStorage({
					key:"gameStatus",
					data:"success"
				})
				uni.redirectTo({
					url:"/pages/game/game"
				})
			  }
			  //抽奖开始
			   if(dataObj.data == "lotteryStart"){
				 uni.setStorage({
				 	key:"lotteryBegan",
				 	data:true
				 })
				 uni.redirectTo({
				 	url:"/pages/list/list"
				 })
			   }
			  
			  // 游戏重置
			  if(dataObj.data == 'resetGame'){
				  // console.log(0)
			    // 删除 gameStatus游戏开始
				// 删除 successOnceGame 游戏已进行过一次
				// 删除 bestTime 最佳用时
				uni.removeStorage({
				    key: 'gameStatus',
				    success: function (res) {
				        // console.log('success gameStatus');
				    }
				});
				uni.removeStorage({
				    key: 'successOnceGame',
				    success: function (res) {
				        // console.log('success successOnceGame');
				    }
				});
				uni.removeStorage({
				    key: 'bestTime',
				    success: function (res) {
				        // console.log('success bestTime');
				    }
				});
				uni.redirectTo({
					url:"/pages/game/game"
				})
			  }
			  // 抽奖重置
			  if(dataObj.data == "lotteryReset"){
				uni.removeStorage({
					key:"lotteryBegan",
					success(){
						console.log("删除成功")
					},
					fail(){
						console.log("未删除")
					}
				})
				uni.redirectTo({
					url:"/pages/list/list"
				})
			  } 
			  // 签到重置
			  if(dataObj.data == "signRest"){
			  // 删除 签到信息sys_userInfo
			  // 删除 已经登陆isLogin
			  
			  // 删除 微信授权userInfo
			  uni.removeStorage({
				  key: 'sys_userInfo', // console.log('success gameStatus');
				  
			  });
			  uni.removeStorage({
				  key: 'isLogin',
			  });
			  uni.removeStorage({
				  key:"userInfo"
			  })
			    uni.redirectTo({
			    	url:"/pages/login/login"
			    })
			  }
			});
			
			// 关闭 WebSocket 连接
			// 注意这里有时序问题，
			// 如果 uni.connectSocket 还没回调 uni.onSocketOpen，而先调用 uni.closeSocket，那么就做不到关闭 WebSocket 的目的。
			// 必须在 WebSocket 打开期间调用 uni.closeSocket 才能关闭。
			// uni.onSocketOpen(function () {
			//   uni.closeSocket();
			// });
			
			uni.onSocketClose(function (res) {
			  console.log('WebSocket 已关闭！');
			});
		},
		data() {
			return {
				
			}
		},
		methods: {
			
		}
	}
</script>

<style scoped lang="less">
.home-container{
	position:relative;
	width:100vw;
	height:100vh;
	overflow:hidden;
	.home-container-list{
		width:88%;
		height:50%;
		position:absolute;
		left:50%;
		top:calc(50% + 13%);
		transform: translate(-50%, -50%);
	}
	.home-container-packet{
		position:fixed;
		width:150rpx;
		height:250rpx;
		right:0px;
		top:calc(50% + 50rpx);
	}
	.home-container-bgFill{
		position:absolute;
		left:0px;
		bottom:0px;
		width:100vw;
		height:80rpx;
		background-image:linear-gradient(0deg, #CC252C 0%, rgba(204, 37, 44, 0) 100%);
	}
}
</style>
